import Vue from 'vue';
import Router from 'vue-router';
import UserLogin from '../components/UserLogin.vue'; // 确保路径正确
import '../assets/css/global.css' //全局格式css
import HomePage from '@/components/HomePage.vue';
import HomeWelcome from '@/components/HomeWelcome.vue';
import UserForm from '@/components/User/UserForm.vue';
import RightsPower from '@/components/power/RightsPower.vue';
import RolesUser from '@/components/power/RolesUser.vue';
import GoodsCate from '@/components/goods/GoodsCate.vue';
Vue.use(Router);

const router = new Router({
  routes: [
    { path: '/', redirect: '/login' },//访问跳转至登录界面
    { path: '/login', component: UserLogin },//链接登录界面地址
    { path: '/home',
      component: HomePage,
      redirect:'/welcome',//跳转至welcome页面
      children:[
        {path: '/welcome', component: HomeWelcome},//子网址（内嵌式）
        {path:'/users', component:UserForm},
        {path:'/rights',component:RightsPower},
        {path:'/roles',component:RolesUser},
        {path:'/categories',component:GoodsCate}
      ] 
    },
  ]
});

// 挂载路由导航守卫
router.beforeEach((to,from,next) => {
    //to--申请访问路径
    //from 从哪个路径跳转来的
    //next 放行函数
    if(to.path === '/login') return next();
    // token 
    const tokenStr = window.sessionStorage.getItem('token')
    if(!tokenStr) return next('/login')
      next()
});// 检测token以防止不通过login页面非法进去主页面

export default router